<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

	class WxmeetingController extends AdminBaseController
	{

		
		/*场地列表*/
		public function index(){
			$where=array();
			//var_dump($_POST);
			//$where['s.status']=1;	
			if(session('incubator_id')){
				$where['s.incubator_id']=session('incubator_id');
			}	
			//$where['s.incubator_id']=1;	
			$site_types = $this->request->param('site_type_id','');

			if($site_types){
				$where['site_type_id'] = $site_types;	
			}
			/**搜索条件**/
			$staff_name = $this->request->param('staff_name','');
			$audit_status = $this->request->param('audit_status',0);
			$site_name = $this->request->param('site_name','');
			$address = $this->request->param('address');
			$start_time = $this->request->param('start_time','');
			$end_time = $this->request->param('end_time','');

			// if ($site_name) {
			// 	$where['site_name'] = ['like', "%$site_name%"];
			// }

			if ($address) {
				$where['s.address'] = ['like', "%$address%"];
			}
			$staff_name = $this->request->param('staff_name');
			// if ($staff_name) {
			// 	$where['m.staff_name'] = ['like', "%$staff_name%"];
			// }
			$site_name = $this->request->param('site_name');
			// if ($site_name) {
			// 	$where['s.site_name'] = ['like', "%$site_name%"];
			// }
			//var_dump($where);
			//var_dump($where);
			// $list=Db::name('site')->alias('s')->where($where)->join('incubator i','i.id=s.incubator_id')->field('s.id,s.incubator_id,s.site_name,s.site_type_id,s.price,s.address,s.people,s.user_name,s.mobile,i.tel,s.site_sketch,s.pic,s.sort,s.is_rent,s.start_time,s.end_time,s.status,i.pic_url,i.incubator_name')->order('id','desc')->paginate(5);
			// $condition=' '
			$condition=1;
			if(!empty($staff_name)  ){
				session('staff_name',$staff_name);
			}
			if(empty($staff_name) && empty($_GET['page'])){
				session('staff_name',null);
			}


			if(session('staff_name')){
				$condition .= ' and  m.staff_name like "%'.session('staff_name').'%"    ';
			}	
			if(!empty($site_name)  ){
				session('site_name',$site_name);
			}
			if(empty($site_name) && empty($_GET['page'])){
				session('site_name',null);
			}


			if(session('site_name')){
				$condition .=' and s.site_name like "%'.session('site_name').'%"  ';
			}	
			if(!empty($start_time)  ){
				session('start_time',$start_time);
			}
			if(empty($start_time) && empty($_GET['page'])){
				session('start_time',null);
			}


			if(session('start_time')){
				$condition .=' and  a.start_time >= "'.strtotime(session('start_time')).'"    ';
			}	
			if(!empty($end_time)  ){
				session('end_time',$end_time);
			}
			if(empty($end_time) && empty($_GET['page'])){
				session('end_time',null);
			}


			if(session('end_time')){
				$condition .=' and a.end_time <= "'.strtotime(session('end_time')).'"    ';
			}	
			if(!empty($audit_status)  ){
				session('audit_status',$audit_status);
			}
			if(empty($audit_status) && empty($_GET['page'])){
				session('audit_status',null);
			}


			if(session('audit_status')){
				if(session('audit_status')==4){
					$condition .= ' and  a.audit_status = 3  and a.is_using = 6 ';
				}else{
					$condition .= ' and  a.audit_status = "'.$audit_status.'"   and a.is_using != 6 ';
				 }
				
			}	
			//echo $condition;
			//exit;
			$list=db('site_apply_info')->alias('a')->join('site s', 'a.site_id=s.id')->join('staff_member m','a.staff_id=m.id')->field('a.start_time,a.end_time,a.is_using,a.audit_status,a.remark,a.is_audit,s.site_name,s.pic,m.staff_name,c.customer_name,m.business_card,a.id')->join('customer c','m.customer_id=c.id')->where($condition)->order('a.audit_status','asc')->paginate(5);
			//echo db('site_apply_info')->getLastSql();
			// $list=json_decode(json_encode($list),true);
			// var_dump($list);

			// var_dump($list[0]);
			//exit;
			$page=$list->render();
			// $site_type=$this->site_type();
			$this->assign('start_time',$start_time);
			$this->assign('end_time',$end_time);
			$this->assign('site_name',$site_name);
			$this->assign('staff_name',$staff_name);
			$this->assign("audit_status", $audit_status);
			$this->assign('address',$address);
			$this->assign("page", $page);
			// $this->assign('site_type',$site_type);
			$this->assign("list", $list);
			//exit;
			return $this->fetch();
		}
		function add_audit(){
			$data['id']= $this->request->param('id',0,'intval');
			if($_POST['cancel']==1){
				$data['audit_status']=2;		
			}else{
				$data['audit_status']=3;	
			}
			

			$audit=Db::name('site_apply_info')->update($data);
		    return $audit;
		}

		/*园区添加*/		
		public function addMeeting(){
			if($_POST){
				
				$data['customer_id'] = $this->request->param('customer_id',0);
				$data['staff_name'] = $this->request->param('staff_name');
				$data['staff_phone'] = $this->request->param('staff_phone','0');
				$data['is_use'] = $this->request->param('is_use',1);
				$data['staff_type'] = $this->request->param('staff_type',2);
				$data['add_time']=time();
				
				$add=Db::name('customer')->insert($data);

				if($add){
					$this->success('添加成功','Customer/index');
				}else{
					$this->error('添加失败');
				}	
			}else{
				
				$where=array();	
				$customers=db('customer')->field('id,customer_name')->slelect();
				$this->assign('customers',$customers);
				return $this->fetch();
			}






		}
		public function addCustomers(){
			if($_POST){
				$salesman=$this->request->param('salesman',0);
				$data['customer_name'] = $this->request->param('customer_name','');
				$data['customer_type'] = $this->request->param('customer_type',0);
				$data['linkman'] = $this->request->param('linkman','');
				$data['mobile'] = $this->request->param('mobile',0);
				$data['salesmobile'] = $this->request->param('salesmobile',0);
				$data['position'] = $this->request->param('position','');
				$data['field'] = $this->request->param('field',0);
				$data['stage'] = $this->request->param('stage',0);
				$data['address'] = $this->request->param('address','');
				$data['source'] = $this->request->param('source','');
				$data['follow_state'] = $this->request->param('follow_state',0);
				$data['follow_stage'] = $this->request->param('follow_stage',0);
				$data['salesman'] = get_user_name($salesman);
				$data['sales_id'] = $salesman;

				$data['salesmobile'] = $this->request->param('salesmobile',0);
				$data['remark'] = $this->request->param('remark','');
				$data['file'] = $this->request->param('file','');
				$data['other_link'] = $this->request->param('other_link',0);
				$data['incubator_id']=session('incubator_id');
				if(!session('incubator_id')){
					$data['incubator_id']=db('user')->where('id',$salesman)->value("incubator_id");
				}
				$data['create_time']=date('Y-m-d H:i:s');
			// echo $incubator_name ;
			// if($type_id==1){
			// 	$station_	
			// }					

			// echo $type_id;
					// var_dump($data);
					// exit;
				$add=Db::name('customer')->insert($data);

				if($add){
					$this->success('添加成功','order/addOrder');
				}else{
					$this->error('添加失败');
				}	
			}else{
				$where=array();	
				if(session('incubator_id')){
					$where['incubator_id']=session('incubator_id');
				}
				$stages=get_cate_list(10);
				// var_dump($stages);
				// if(session('role_id')==3){
				// 	$where['u.id']=session('ADMIN_ID');
				// }
				if(session('role_id')==3){
					$where['u.id']=session('ADMIN_ID');
					$this->assign('role',3);
				}else{
					$this->assign('role',500);
				}
				$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
				//var_dump($salesman);
				$gen_stage=get_cate_list("15");
				$link_way=get_cate_list(11);
				$namess=session('name');
				$this->assign('namess',$namess);
				$this->assign('link_way',$link_way);		
				$this->assign("gen_stage",$gen_stage);
				$this->assign("stages",$stages);
				$this->assign('salesman',$salesman);
				$cate_list=get_cate_list(6);
				$this->assign('cate_list',$cate_list);
				return $this->fetch();
			}






		}
		/*详情*/
		public function detailCustomer(){
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['id']= $id;
			}else{
				// dump(info_back('','id为空',false));
				// exit;
				return info_back('','id为空',false);
			}
			$detail= Db::name('customer')->where($where)->find();
			 // dump($detail);
			// echo Db::name('station')->getLastSql();
			$where=array();	
			if(session('incubator_id')){
				$where['incubator_id']=session('incubator_id');
			}
			if(session('role_id')==3){
				$where['u.id']=session('ADMIN_ID');
			}
			$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
				//var_dump($salesman);
			$cate_list=get_cate_list(6);
			$stages=get_cate_list(10);
			$gen_stage=get_cate_list("15");
			$link_way=get_cate_list(11);
			$this->assign('link_way',$link_way);		
			$this->assign("gen_stage",$gen_stage);
			$this->assign('stages',$stages);
			$this->assign('cate_list',$cate_list);
			$this->assign('salesman',$salesman);
			$this->assign('detail',$detail);
			return $this->fetch();
		}

		/*编辑园区*/	
		public function editCustomer(){

			if($_POST){
				// var_dump($_POST);
				// exit;
				unset($_POST['snames']);
				$_POST['sales_id']=$_POST['salesman'];
				$_POST['salesman']=get_user_name($_POST['salesman']);
				// var_dump($_POST);
				// exit;	
				$_POST['incubator_id']=session('incubator_id');
				$edit = Db::name('customer')->update($_POST);
				$this->success('编辑成功','index');	
				if($edit){
					$this->success('编辑成功','index');

				}
			}
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['id']= $id;
			}else{
				return info_back('','id不能为空',false);
			}
			$detail= Db::name('customer')->where($where)->find();
			//dump($detail);
			// exit;
			$where=array();	
			if(session('incubator_id')){
				$where['incubator_id']=session('incubator_id');
			}
			// if(session('role_id')==3){
			// 	$where['u.id']=session('ADMIN_ID');
			// }
			if(session('role_id')==3){
				$where['u.id']=session('ADMIN_ID');
				$this->assign('role',3);
			}else{
				$this->assign('role',500);
			}
			$salesman=db('user')->alias('u')->join('roleUser r','u.id=r.user_id')->where('role_id',3)->where($where)->field('u.id,u.user_login')->select();
				//var_dump($salesman);
			$stages=get_cate_list(10);
			$gen_stage=get_cate_list("15");
			$link_way=get_cate_list(11);
			$namess=session('name');
			$this->assign('namess',$namess);
			$this->assign('link_way',$link_way);		
			$this->assign("gen_stage",$gen_stage);
			$this->assign('stages',$stages);
			$this->assign('salesman',$salesman);
			$this->assign('detail',$detail);
			$cate_list=get_cate_list(6);
			$this->assign('cate_list',$cate_list);
			return $this->fetch();

		}


		/*删除园区*/
		public function delCustomer(){
			$data['id']= $this->request->param('id',0,'intval');

			$data['status']=0;

			$del=Db::name('customer')->update($data);
			if($del){
				return info_back('','删除成功',true);
			}else{
				return info_back('','删除失败',false);
			}

		}	



		/*验证业务员*/

		public function check_sales(){
			$name=$this->request->param('name','');
			$sure=$this->request->param('sure','');
			if($sure){

			}
			$result=db('user')->where('user_login',$name)->find();
			if($result){
				return 1;
			}else{
				return 0;
			}

		}	



		function meeting_detail(){
			$list=array();
			$this->assign('list',$list);
			return $this->fetch();
		}




		function SendInfo(){
			$data=array();
			$data['keyword1'] = $this->request->param('keyword1','暂无');
			$data['keyword2'] = $this->request->param('keyword2','暂无');
			$data['keyword3'] = $this->request->param('keyword3','暂无');
			$data['keyword4'] = $this->request->param('keyword4','暂无');
			$data['page'] =  1;
			$data1['staff_id_from']=   $this->request->param('from_id');
			$data1['staff_id_accept']=   $this->request->param('accept_id');
			// $data['user_id']=db('staff_member')->where('id',$data1['staff_id_accept'])->value('app_userid');
			$data['user_id']= 451;
			$data['form_id']= $this->request->param('form_id');
			$data['token']=   $this->request->param('token');
			$url= 'http://v2.crejoy.cn/user/send_msg/weixin_yuyue';
			
			$info = sqcurl($url, 'post', $data, array());
			return $info;


		}











	}